<style lang="less">
    @import "batchDispatch.less";
</style>
<template>
    <div class="search">
        <Row>
            <Col>
                <Card>
                    <Row class="operation">
                        <Col span="6" style="padding-right:10px">
                            <Select v-model="currentDepartment" filterable>
                                <Option v-for="item in departmentList" :value="item" :key="item">{{ item }}</Option>
                            </Select>
                        </Col>
                        <Col span="6" style="padding-right:10px">
                            <Select v-model="currentDepartment" filterable>
                                <Option v-for="item in departmentList" :value="item" :key="item">{{ item }}</Option>
                            </Select>
                        </Col>
                        <Col span="6" style="padding-right:10px">
                            <Select v-model="currentDepartment" filterable>
                                <Option v-for="item in departmentList" :value="item" :key="item">{{ item }}</Option>
                            </Select>
                        </Col>
                        <Button @click="add" type="primary">查询</Button>
                        <Button @click="add" type="primary">批量下发</Button>
                    </Row>
                    <Row>
                        <Alert show-icon>
                            已选择 <span class="select-count">{{selectCount}}</span> 项
                            <a class="select-clear" @click="clearSelectAll">清空</a>
                        </Alert>
                    </Row>
                    <Row>
                        <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom"
                               @on-sort-change="changeSort" @on-selection-change="changeSelect"></Table>
                    </Row>
                    <Row type="flex" justify="end" class="page">
                        <Page :current="pageNumber" :total="total" :page-size="pageSize" @on-change="changePage"
                              @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total
                              show-elevator show-sizer></Page>
                    </Row>
                </Card>
            </Col>
        </Row>

        <!--对话框-->
        <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="500">
            <Form ref="form" :model="form" :label-width="80" :rules="formValidate">
                <FormItem label="终端名称" prop="name">
                    <Input v-model="form.name"/>
                </FormItem>
                <FormItem label="序列号" prop="serial">
                    <Input v-model="form.serial"/>
                </FormItem>
                <FormItem label="所属机构" prop="belonged">
                    <Select v-model="form.belonged">
                        <Option value="image">OPPO</Option>
                        <Option value="video">华为</Option>
                        <Option value="video1">小米</Option>
                    </Select>
                </FormItem>
                <FormItem label="开始时间" prop="startTime">
                    <DatePicker :value="form.startTime" format="yyyy年MM月dd日" type="date"
                                placeholder="Select date"
                                @on-change="handleStartChange"
                                style="width: 200px"></DatePicker>
                </FormItem>
                <FormItem label="结束时间" prop="endTime">
                    <DatePicker :value="form.endTime" format="yyyy年MM月dd日" type="date"
                                placeholder="Select date"
                                @on-change="handleEndChange"
                                style="width: 200px"></DatePicker>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" @click="handleCancel">取消</Button>
                <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    export default {
        name: "simple-table",
        data() {
            return {
                currentDepartment: "",
                loading: true, // 表单加载状态
                sortColumn: "createTime", // 排序字段
                sortType: "desc", // 排序方式
                modalType: 0, // 添加或编辑标识
                modalVisible: false, // 添加或编辑显示
                modalTitle: "", // 添加或编辑标题
                form: {
                    // 添加或编辑表单对象初始化数据
                    name: "",
                    serial: "",
                    belonged: "",
                    startTime: "",
                    endTime: "",
                },
                // 表单验证规则
                formValidate: {
                    name: [{required: true, message: "不能为空", trigger: "blur"}],
                    serial: [{required: true, message: "不能为空", trigger: "blur"}],
                    belonged: [{required: true, message: "不能为空", trigger: "blur"}],
                    startTime: [{required: true, message: "不能为空", trigger: "blur"}],
                    endTime: [{required: true, message: "不能为空", trigger: "blur"}]
                },
                submitLoading: false, // 添加或编辑提交状态
                selectList: [], // 多选数据
                selectCount: 0, // 多选计数
                columns: [
                    // 表头
                    {
                        type: "selection",
                        width: 60,
                        align: "center"
                    },
                    {
                        title: "序号",
                        type: "index",
                        width: 70,
                        align: "center"
                    },
                    {
                        title: "终端名称",
                        key: "name",
                        sortable: true
                    },
                    {
                        title: "序列号",
                        key: "serial",
                        sortable: true,
                        width: 120,
                        sortType: "desc",
                        align: "center"
                    },
                    {
                        title: "所属机构",
                        key: "belonged",
                        sortable: true,
                        width: 120,
                        sortType: "desc",
                        align: "center"
                    },
                    {
                        title: "开始时间",
                        key: "createTime",
                        sortable: true,
                        width: 110,
                        sortType: "desc",
                        align: "center"
                    },
                    {
                        title: "结束时间",
                        key: "endTime",
                        sortable: true,
                        width: 110,
                        sortType: "desc",
                        align: "center"
                    },
                    {
                        title: "操作",
                        key: "action",
                        align: "center",
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "primary",
                                            size: "small",
                                            icon: "ios-create-outline"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    "编辑"
                                ),
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "error",
                                            size: "small",
                                            icon: "md-trash"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.remove(params.row);
                                            }
                                        }
                                    },
                                    "删除"
                                ),
                                h(
                                    "Button",
                                    {
                                        props: {
                                            // type: "error",
                                            size: "small"
                                            // icon: "md-trash"
                                        },
                                        on: {
                                            click: () => {
                                                this.remove(params.row);
                                            }
                                        }
                                    },
                                    "已关联商品"
                                )
                            ]);
                        }
                    }
                ],
                data: [], // 表单数据
                pageNumber: 1, // 当前页数
                pageSize: 10, // 页面大小
                total: 0, // 表单数据总数
                searchData: "", //搜索数据
                departmentList: [  //部门数据
                    "中山华为专区0",
                    "广州华为专区1",
                    "深圳华为专区2"]
            };
        },
        methods: {
            init() {
                this.getDataList();
            },
            changePage(v) {
                this.pageNumber = v;
                this.getDataList();
                this.clearSelectAll();
            },
            changePageSize(v) {
                this.pageSize = v;
                this.getDataList();
            },
            changeSort(e) {
                this.sortColumn = e.key;
                this.sortType = e.order;
                if (e.order === "normal") {
                    this.sortType = "";
                }
                this.getDataList();
            },
            handleStartChange(daterange) {
                this.form.startTime = daterange;
            },
            handleEndChange(daterange) {
                this.form.endTime = daterange;
            },
            getDataList() {
                this.loading = true;
                let params = {
                    pageNumber: this.pageNumber,
                    pageSize: this.pageSize,
                    sort: this.sortColumn,
                    order: this.sortType
                };
                // 请求后端获取表单数据 请自行修改接口
                // this.getRequest("请求路径", params).then(res => {
                //   this.loading = false;
                //   if (res.success === true) {
                //     this.data = res.result.content;
                //     this.total = res.result.totalElements;
                //   }
                // });
                // 以下为模拟数据
                this.data = [
                    {
                        id: "1",
                        name: "中山二华为专区",
                        serial: "QERDSCDDYE",
                        belonged: "中山二营业厅",
                        createTime: "2018-10-12",
                        endTime: "2020-10-12"
                    }, {
                        id: "2",
                        name: "中山二华为专区",
                        serial: "QERDSCDDYE",
                        belonged: "中山二营业厅",
                        createTime: "2018-10-12",
                        endTime: "2020-10-12"
                    }, {
                        id: "3",
                        name: "中山二华为专区",
                        serial: "QERDSCDDYE",
                        belonged: "中山二营业厅",
                        createTime: "2018-10-12",
                        endTime: "2020-10-12"
                    }, {
                        id: "4",
                        name: "中山二华为专区",
                        serial: "QERDSCDDYE",
                        belonged: "中山二营业厅",
                        createTime: "2018-10-12",
                        endTime: "2020-10-12"
                    }

                ];
                this.total = this.data.length;
                this.loading = false;
            },
            handleCancel() {
                this.modalVisible = false;
            },
            handleSearch(data) {
                console.log(data)
            },
            handleSubmit() {
                this.$refs.form.validate(valid => {
                    if (valid) {
                        this.submitLoading = true;
                        if (this.modalType === 0) {
                            // 添加 避免编辑后传入id等数据 记得删除
                            delete this.form.id;
                            // this.postRequest("请求地址", this.form).then(res => {
                            //   this.submitLoading = false;
                            //   if (res.success === true) {
                            //     this.$Message.success("操作成功");
                            //     this.getDataList();
                            //     this.modalVisible = false;
                            //   }
                            // });
                            // 模拟请求成功
                            this.submitLoading = false;
                            this.$Message.success("操作成功");
                            this.getDataList();
                            this.modalVisible = false;
                        } else {
                            // 编辑
                            // this.postRequest("请求地址", this.form).then(res => {
                            //   this.submitLoading = false;
                            //   if (res.success === true) {
                            //     this.$Message.success("操作成功");
                            //     this.getDataList();
                            //     this.modalVisible = false;
                            //   }
                            // });
                            // 模拟请求成功
                            this.submitLoading = false;
                            this.$Message.success("操作成功");
                            this.getDataList();
                            this.modalVisible = false;
                        }
                    }
                });
            },
            add() {
                this.modalType = 0;
                this.modalTitle = "添加终端";
                this.$refs.form.resetFields();
                delete this.form.id;
                this.modalVisible = true;
            },
            edit(v) {
                this.modalType = 1;
                this.modalTitle = "编辑";
                // 转换null为""
                for (let attr in v) {
                    if (v[attr] === null) {
                        v[attr] = "";
                    }
                }
                let str = JSON.stringify(v);
                let data = JSON.parse(str);
                this.form = data;
                this.modalVisible = true;
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    // 记得确认修改此处
                    content: "您确认要删除 " + v.name + " ?",
                    onOk: () => {
                        // 删除
                        // this.deleteRequest("请求地址，如/deleteByIds/" + v.id).then(res => {
                        //   if (res.success === true) {
                        //     this.$Message.success("操作成功");
                        //     this.getDataList();
                        //   }
                        // });
                        // 模拟请求成功
                        this.$Message.success("操作成功");
                        this.getDataList();
                    }
                });
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            changeSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            delAll() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择要删除的数据");
                    return;
                }
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
                    onOk: () => {
                        let ids = "";
                        this.selectList.forEach(function (e) {
                            ids += e.id + ",";
                        });
                        ids = ids.substring(0, ids.length - 1);
                        // 批量删除
                        // this.deleteRequest("请求地址，如/deleteByIds/" + ids).then(res => {
                        //   if (res.success === true) {
                        //     this.$Message.success("操作成功");
                        //     this.clearSelectAll();
                        //     this.getDataList();
                        //   }
                        // });
                        // 模拟请求成功
                        this.$Message.success("操作成功");
                        this.clearSelectAll();
                        this.getDataList();
                    }
                });
            }
        },
        mounted() {
            this.init();
        }
    };
</script>
